package com.iplant.mes.controller.sfc;

import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.controller.BasicController;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.mes.service.SFCService;
import com.iplant.mes.service.po.sfc.SFCLoginEvent;
import com.iplant.mes.service.po.sfc.SFCTaskSelf;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/SFCLoginEvent")
public class SFCLoginEventController extends BasicController {
    private static final Logger logger = LoggerFactory.getLogger(SFCLoginEventController.class);

    public SFCLoginEventController() {
        super(SFCLoginEvent.class);
    }

    @Autowired
    SFCService mSFCService;

    /**
     * 批量开工打卡
     */
    @PostMapping("/BatchClock")
    public Object BatchClock(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Map<String, Object> wResult = new HashMap<String, Object>();
        try {
            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            List<SFCTaskSelf> wSFCTaskSelfList = CloneTool.CloneArray(wParam.get("data"), SFCTaskSelf.class);
            if (wSFCTaskSelfList == null || wSFCTaskSelfList.size() == 0) {
                return GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
            }

            ServiceResult<Integer> wServiceResult = mSFCService.SFC_BatchClock(wLoginUser, wSFCTaskSelfList);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }
}
